package cn.tedu.egweibo05.controller;

import cn.tedu.egweibo05.base.response.JsonResult;
import cn.tedu.egweibo05.mapper.CommentMapper;
import cn.tedu.egweibo05.pojo.dto.CommentDTO;
import cn.tedu.egweibo05.pojo.entity.Comment;
import cn.tedu.egweibo05.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/comment")
@Api(tags = "评论管理模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
@PostMapping("/insert")
@ApiOperation("发布评论")
@ApiImplicitParam(name = "commentDTO", value = "评论数据", required = true, dataType = "CommentDTO")
    public JsonResult insertComment(@RequestBody CommentDTO commentDTO , @ApiIgnore HttpSession session) {
    UserVO userVO = (UserVO) session.getAttribute("user");
    Comment comment = new Comment();
    BeanUtils.copyProperties(commentDTO, comment);
    comment.setCreated(new Date());
    comment.setUserId(userVO.getId());
    commentMapper.insertComment(comment);
    return JsonResult.ok();
    }

@GetMapping("/selectByWeiboId/{wid}")
@ApiOperation("查询指定微博的所有评论")
@ApiImplicitParam(name = "wid", value = "微博id", required = true, dataType = "Long")
    public JsonResult selectCommentByWeiboId(@PathVariable Long wid) {
        return JsonResult.ok(commentMapper.selectByWeiboId(wid));
    }

}
